<?php
/**
 * @author Inhere
 * @version v1.0
 * Use : String
 * Date : 2015-1-5
 */
namespace ulue\libs\validate;

use ulue\libs\validate\Validate;

class String
{
    private $string = NULL;
    public $mode    = self::WARM_TEST;

    const HIGH_TEST = 1; // 严格 检查类型
    const WARM_TEST = 2; // 温和 忽略类型 string number

    public function __construct($input, $mode = NULL)
    {
        if ( $this->isString($input, $mode) ) {
            $this->string = $string;
        }
    }

    static public function owner($input, $mode = NULL)
    {
        static $owner = NULL;

        if (is_null($owner)){
            $owner = new self($string, $mode);
        }

        return $owner;
    }

    public function limit( $input=NULL, $min=1, $max=255, $charset = 'utf-8')
    {
        $input  = $this->_getInputString($input);
        $strlen = strlen($input);

        if ( $strlen>=$min && $strlen<=$max ) {
            return false;
        }

        return true;

    }

    /**
     * [verifyOptions description]
     *  'notEmpty && isUrl || isNumber'
     * @return [type] [description]
     */
    public function verifyOptions()
    {
        foreach ($variable as $key => $value) {
            # code...
        }
    }

    public function isString( $input, $mode = NULL)
    {
        if ( $mode == self::WARM_TEST ) {
            return is_string($input) || is_numeric($input);
        } else if ( $mode == self::HIGH_TEST ) {
            return is_string($input);
        }

        return false;
    }

    public function isEmpty( $input = NULL, $mode = NULL )
    {
        return !$this->notEmpty( $input, $mode);
    }

    public function notEmpty( $input = NULL, $mode = NULL )
    {
        $input  = $this->_getInputString($input);
        $mode   = $this->_getInputMode($mode);

        if ( !$this->isString($input, $mode) ) {
            return false;
        }

        if ( empty($input) ) {
            return false;
        }

        return true;
    }

    public function isUrl($input)
    {
        return Validate::url($input);
    }

    public function isEmail($input)
    {
        return Validate::email($input);
    }

    public function isEnglish($input)
    {
        return Validate::english($input);
    }

    public function isNumber($input)
    {
        return Validate::regex($input,'number');
    }

    public function isInteger($input)
    {
        return Validate::regex($input,'integer');
    }

    public function isDouble($input)
    {
        return Validate::regex($input,'double');
    }

///////////////////////////////////


    public function setMode($value)
    {
        if ( in_array($value, [self::WARM_TEST, self::HIGH_TEST]) )
        {
            $this->mode = $value;
        }

    }

    public function set($input)
    {
        if ( $this->isString($input) ) {
            $this->string = $string;
        }

        throw new \DDataTypeException("请传入合法的数据类型！");
    }

    public function get()
    {
        return $this->string;
    }

    private function _getInputString($input)
    {
        if ( is_null($input) ) {
            return $this->string;
        } else {
            return $this->string = $input;
        }
    }

    private function _getInputMode($mode)
    {
        if ( is_null($mode) ) {
            return $this->mode;
        } else {
            return $this->mode = $mode;
        }
    }


}
